﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using CSharpDesign.DAL;
using CSharpDesign.BLL;

namespace CSharpDesign.Admin
{
    public partial class AdminList : System.Web.UI.Page
    {
        SqlHelper data = new SqlHelper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                Get_Info();
            }
        }
        protected void gvInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SqlHelper mydata = new SqlHelper();

            string ID = gvInfo.DataKeys[e.RowIndex].Values[0].ToString();
            try
            {

                if (int.Parse(data.ReturnSql("select count(*) from  Admin ")) <= 1)
                {
                    Label4.Text = "必须保留一个账号！！";
                }
                else
                {

                    mydata.RunSql("delete  from Admin  where  id='" + ID + "'");

                    Label4.Text = "删除成功！";
                    gvInfo.EditIndex = -1;
                    Get_Info();

                }
            }
            catch
            {

                Label4.Text = "删除失败！";
            }

        }



        private void Get_Info()
        {
            try
            {


                gvInfo.DataSource = GetCodeBy(0);
                gvInfo.DataBind();
            }
            catch
            {

            }

        }
        protected void gvInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvInfo.PageIndex = e.NewPageIndex;
            Get_Info();
        }
        protected void gvInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            AdminListService als = new AdminListService();
            //鼠标移动变色
            als.Color(e);

        }
        public DataSet GetCodeBy(int iCount)
        {
            AdminListService als = new AdminListService();
            SqlHelper date = new SqlHelper();
            string count = iCount.ToString();
            string sql = als.SelectAdmin(iCount, count);
            SqlConnection con = new SqlConnection(SqlHelper.connstring);
            SqlCommand cmd = new SqlCommand(sql, con);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = null;
            try
            {
                con.Open();
                ds = new DataSet();
                da.Fill(ds);

            }
            catch (SqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
            return ds;
        }

    }

}